<?php

namespace App\Admin\Controllers;

use App\Models\Semester;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;


class SemesterController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('学期管理')
            ->description('学期信息')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('学期管理')
            ->description('查看学期')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('学期管理')
            ->description('修改学期')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('学期管理')
            ->description('新建学期')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Semester);
        $grid->model()->orderBy('id','desc');
        $grid->id('Id')->sortable();
        $grid->name('学期名称');
        $grid->start_date('开始日期');
        $grid->end_date('结束日期');
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '否', 'color' => 'default'],
        ];
        $grid->is_semester('是否当前学期')->switch($states);
//        $grid->is_semester()->editable('select', [1 => 'option1', 2 => 'option2', 3 => 'option3']);
        $grid->created_at('创建日期');
        $grid->updated_at('修改日期');

        $grid->paginate(10);
        $grid->disableFilter();
        $grid->actions(function ($actions) {
//            $actions->disableDelete();
//            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Semester::findOrFail($id));

        $show->name('学期名称');
        $show->start_date('开始日期');
        $show->end_date('结束日期');
        $show->is_semester('Is semester');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Semester);

        $form->text('name', '学期名称')->default('yyyy-yyyy 第几学期')->setWidth(2, 2)->rules('required', [
            'required' => '学期名称必填'
        ]);
        $form->date('start_date', '开始日期')->default(date('Y-m-d'));
        $form->date('end_date', '结束日期')->default(date('Y-m-d'));
        $form->switch('is_semester', '是否当前学期')->states([ 'on'  => ['value' => 1, 'text' => '是'], 'off' => ['value' => 0, 'text' => '否']]);

        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        //判断新建数据还是修改数据，新建数据对学期名称进行校验
        $form->saving(function (Form $form) {
            $id = $form->model()->id;

            //判断是否有id
            if(!$id){
                $request = request()->all();
                $name = $request['name'];
                $semester = Semester::where('name', $name)->first();
                if(!empty($semester) ){
                    $title = '提示';
                    $message = '学期名称已存在';
                }

                if(!empty($title)){
                    $error = new MessageBag([
                        'title'   => $title,
                        'message' => $message,
                    ]);
                    return back()->with(compact('error'))->withInput();
                }
            }
        });

        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $semester = $form->model()->is_semester;

            if($semester) {
                $form->model()->where('id', '<>', $id)->update(['is_semester' => 0]);
            }
        });

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        return $form;
    }
}
